﻿using ProjectSemIII.Administrator.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Support
{
    public partial class EditFAQ : System.Web.UI.Page
    {
        private FAQSupportProcess process;
        private Entity.FAQ faq;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.Count == 0 || Request.QueryString.Count > 1)
            {
                ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.close();", true);
            }
            else
            {
                process = new FAQSupportProcess();
                if (!Page.IsPostBack)
                {
                    this.getInfoFAQ();
                }
            }
        }

        protected void ddFAQCatalog_SelectedIndexChanged(object sender, EventArgs e)
        {
            nameChoose.InnerText = ddFAQCatalog.SelectedItem.Text;
        }

        private void getInfoFAQ()
        {
            int FAQID = Convert.ToInt32(Request.QueryString["faqid"]);
            faq = process.getInfoFAQ(FAQID);
            txtQuestion.Text = faq.Question;
            txtAnswers.Text = faq.Answer;
            ddFAQCatalog.SelectedValue = faq.FAQCatalog.ToString();
            nameChoose.InnerText = ddFAQCatalog.SelectedItem.Text;
        }

        protected void btSubmit_Click(object sender, EventArgs e)
        {
            if (checkValidateForm())
            {
                faq = new Entity.FAQ();
                faq.FAQID = Convert.ToInt32(Request.QueryString["faqid"]);
                faq.Question = txtQuestion.Text;
                faq.Answer = txtAnswers.Text;
                faq.FAQCatalog = Convert.ToInt32(ddFAQCatalog.SelectedValue);
                int result = process.updateFAQ(faq);
                if(result == 10){
                    ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.opener.location.reload();window.close();", true);
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in update FAQ process!";
                }
            }
        }

        private bool checkValidateForm()
        {
            bool flag = true;
            if (txtQuestion.Text == "" || txtQuestion.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the question!";
                flag = false;
            }
            else if (txtAnswers.Text == "" || txtAnswers.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the answer!";
                flag = false;
            }
            else if (Convert.ToInt32(ddFAQCatalog.SelectedValue) == -1)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please choose FAQ Catalog!";
                flag = false;
            }
            else if (process.checkQuestionTitle(Convert.ToInt32(ddFAQCatalog.SelectedValue), txtQuestion.Text) == true)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "The same question is exits!";
                flag = false;
            }
            return flag;
        }
    }
}